package com.cty.service.impl;

import com.cty.mapper.ScheduleMapper;
import com.cty.pojo.Schedule;
import com.cty.service.ScheduleService;
import com.cty.utils.PageBean;
import com.cty.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    ScheduleMapper scheduleMapper;
    @Override
    public R page(int pageSize, int currentPage) {
        //分页查询
        PageHelper.startPage(currentPage,pageSize);
        //查询
        List<Schedule> scheduleList = scheduleMapper.queryList();
        //分页装配
        PageInfo<Schedule> info = new PageInfo<>(scheduleList);
        //装配到pageBean,只要当前页的信息
        PageBean<Schedule> pageBean = new PageBean<>(currentPage,pageSize,info.getTotal(),info.getList());
        R ok = R.ok(pageBean);
        return ok;
    }

    @Override
    public R save(Schedule schedule) {
        int rows = scheduleMapper.save(schedule);
        if(rows > 0) {
            return R.ok(schedule);
        }
        return R.fail("保存失败");
    }

    @Override
    public R delete(Integer id) {
        int rows = scheduleMapper.delete(id);
        if (rows > 0) {
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

    @Override
    public R update(Schedule schedule) {
        if (schedule.getId() == null) {
            return R.fail("无法更新，无id值");
        }
        int rows = scheduleMapper.update(schedule);
        if (rows > 0) {
            return R.ok(schedule);
        }
        return R.fail("更新失败");
    }


}
